import { defineStore } from "pinia";
import Cookies from "js-cookie";
import { Session } from "/@/utils/storage";
import { ElMessage } from "element-plus";

let imgList =[]
console.log(import.meta.env.VITE_API_URL)
imgList = [
  `${import.meta.env.VITE_API_URL}header1.png`,
  `${import.meta.env.VITE_API_URL}header2.png`,
  `${import.meta.env.VITE_API_URL}header3.png`,
  `${import.meta.env.VITE_API_URL}header4.jpg`,
]

let randomNum = Math.ceil(Math.random()*3)
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore("userInfo", {
  state: () => ({
    userInfos: {
      userName: "",
      photo: "",
      time: 0,
      roles: [],
      authBtnList: [],
    },
  }),
  actions: {
    async setUserInfos() {
      const userInfos = await this.getApiUserInfo();
      this.userInfos = userInfos;
    },
    
    async getApiUserInfo() {
      return new Promise((resolve) => {
        if (localStorage.getItem("userMessage")) {
          // 模拟数据，请求接口时，记得删除多余代码及对应依赖的引入

          let defaultRoles = [];
          let defaultAuthBtnList = [];
          // admin 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
          let adminRoles = ["admin"];
          // admin 按钮权限标识
          let adminAuthBtnList = ["btn.add", "btn.del", "btn.edit", "btn.link"];
          // test 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
          let testRoles = ["common"];
          // test 按钮权限标识
          let testAuthBtnList = ["btn.add", "btn.link"];
         
          // 用户信息模拟数据
          const userInfos = {
            userName: JSON.parse(localStorage.getItem("userMessage")).user_name,
            photo:
            imgList[randomNum],
            time: new Date().getTime(),
            roles: JSON.parse(localStorage.getItem("userMessage")).roles,
            authBtnList: ["btn.add", "btn.del", "btn.edit", "btn.link"],
          };
          Session.set("userInfo", userInfos);
          resolve(userInfos);
        } else {
          ElMessage({
            type: "error",
            message: "请先登录",
          });
        }
      });
    },
  },
});
